#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#include "harvest_data.au3"
#include "harvest_proc.au3"

Func parse_input_harvest($pos)
   Local $str = input($pos)
   Local $limit[3] = [0,0,0]
   Local $ret = StringRegExp($str,"(\d+)\s+(\d+)",1)
   If @error = 0 Then
	  $limit[1] = $ret[0]
	  $limit[2] = $ret[1]
   ElseIf @error = 2 Then
	  _DebugOut("bad regexp against line2 '" & $str & "'")
	  Exit
   Else
	  _DebugOut("bad line '" & $str & "'")
   EndIf
   Return $limit
EndFunc

Global Const $harvest_stage_1 = 30
Global Const $harvest_stage_1_1 = 50
Global Const $harvest_stage_2 = 60
Global Const $harvest_stage_3 = 80
Global Const $harvest_stage_4 = 110

Global $harvest_error_count = 0

Func harvest($pos)
   Switch $substep[$pos]
	  Case 0
		 scan_field($pos)
		 $stepdata[$pos] = $last_count_result
		 $substep[$pos] += 1
		$harvest_error_count = 0
	  Case 1 To $harvest_stage_1
		 scan_field($pos)
		 If last_result_change($stepdata[$pos]) And $substep[$pos] < $harvest_stage_1 Then
			$stepdata[$pos] = $last_count_result
			$substep[$pos] += 1
		 Else
			Local $cnt = count_last_result()
			Local $limit = parse_input_harvest($pos)
			Local $warn = ""
			If $cnt[1] = 0 And $cnt[2] = 0 Then
			   $warn = " ***************** error founding *****************************"
			   ;Beep()
			   $harvest_error_count += 1
			   If $harvest_error_count > 10 Then
				  StopWork($pos,"error founding")
			   EndIf
			Else
			   For $l = 1 To 2
				  If $limit[$l] > 0 And $cnt[$l] >= $limit[$l] Then
					 message($pos,"found( " & $l & " )= " & $cnt[$l])
					 _DebugOut($pos & "harvesting( " & $l & " )= " & $cnt[$l])
					 $substep[$pos] = $harvest_stage_1 + 1
					 Return True
				  EndIf
			   Next
			EndIf
			_DebugOut($pos & ": found e= " & $cnt[1] & "(" & $limit[1] & ") c= " & $cnt[2] & "(" & $limit[2] & ")" & $warn)
			Return False
		 EndIf
	  Case $harvest_stage_1 + 1
		 _DebugOut($pos & ": start harvest")
		 try_harvest($pos)
		 next_over($pos,500)
		 $substep[$pos] += 1
	  Case $harvest_stage_1 + 2 To $harvest_stage_1_1
		 _DebugOut($pos & ": check harvest")
		 Switch can_harvest($pos)
			Case 0
			   ;exit process
			   StopWork($pos,"no tryes")
			   ;Beep()
			Case 1
			   $substep[$pos] = $harvest_stage_1_1 + 1
			Case Else
			   If $substep[$pos] < $harvest_stage_1_1 Then
				  $substep[$pos] += 1
			   Else
				  $substep[$pos] = $harvest_stage_1 + 1
			   EndIf
		 EndSwitch
	  Case $harvest_stage_1_1 + 1
		 _DebugOut($pos & ": agree harvest")
		 agree_harvest($pos,0) ; agree real
		 next_over($pos,300)
		 $substep[$pos] = $harvest_stage_2
		 make_progress($pos)
	  Case $harvest_stage_2 To $harvest_stage_2 + 15
		 Local $last = try_harvest($pos,$substep[$pos] - $harvest_stage_2)
		 If $last < 0 Then
			$substep[$pos] = $harvest_stage_3
		 Else
			$substep[$pos] = $harvest_stage_2 + $last + 1
			next_over($pos, 300 + random_shift(120))
		 EndIf
	  Case $harvest_stage_3
		 scan_field($pos)
		 $stepdata[$pos] = $last_count_result
		 $substep[$pos] += 1
	  Case $harvest_stage_3 + 1 To $harvest_stage_4
		 scan_field($pos)
		 If last_result_change($stepdata[$pos]) And $substep[$pos] < $harvest_stage_4 Then
			$stepdata[$pos] = $last_count_result
			$substep[$pos] += 1
		 Else
			Local $cnt = count_last_result()
			_DebugOut($pos & ":rest e= " & $cnt[1] & " c= " & $cnt[2] )
			For $l = 1 To 2
			   If $cnt[$l] > 0 Then
				  $substep[$pos] = $harvest_stage_2
				  Return True
			   EndIf
			Next
			Return False
		 EndIf
   EndSwitch
   Return True ; need continue work
EndFunc
